package com.suwenhao.dao;

import com.suwenhao.entity.Meeting;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

/**
 * @Author:suwenhao
 * @Description: 测试
 * @Date: Created in 2020/9/25 11:03
 */
public class MeetingDao extends BaseDao{

    public List<Meeting> listPage(){
        String str="SELECT" +
                " m.*, " +
                " d.NAME deptName " +
                " FROM" +
                " meeting m" +
                " LEFT JOIN dept d ON d.id = m.dept_id";
        return jdbcTemplate.query(str,new BeanPropertyRowMapper<>(Meeting.class));
    }

    public void addMeeting(Meeting meeting) {
        String sql = "insert into meeting(id, dept_id,title,content, publish_date,start_time,end_time,status,make_user) values (null,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,
                meeting.getDeptId(),
                meeting.getTitle(),
                meeting.getContent(),
                meeting.getPublishDate(),
                meeting.getStartTime(),
                meeting.getEndTime(),
                meeting.getStatus(),
                meeting.getMakeUser());
    }

    public void updateStatusById(Meeting meeting) {
        String sql = "update meeting set status=? where id=?";
        jdbcTemplate.update(sql,
                meeting.getStatus(),
                meeting.getId());
    }


    public Meeting getMeetingById(Integer id) {
        String sql = "select * from meeting where id=?";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Meeting.class),id);
        } catch (DataAccessException e) {
            return null;
        }

    }




    public List<Integer> listUserId(Integer id) {
        String sql = "select u_id from meeting_join where u_id=?";
       return jdbcTemplate.queryForList(sql,Integer.class,id);
    }


    public void attendTheMeeting(Integer u_id, Integer c_id) {
        String sql = "insert  into meeting_join(u_id,c_id) vlaues(?,?)";
         jdbcTemplate.update(sql,u_id,c_id);
    }


}
